﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WenSkin.Controls
{
    public class WenButtonTriangle : WenControl
    {


        public WenButtonTriangle() : base()
        {
            this.Size = new Size(32, 32);
            this.BackColor = Color.Transparent;
        }

        #region 私有属性

        private WenButtonTriangleStyle closeStyle = WenButtonTriangleStyle.Right;
        private WenButtonTriangleStyle openStyle = WenButtonTriangleStyle.Bottom;
        private bool state;

        #endregion


        #region 公有属性

        [DefaultValue(WenButtonTriangleStyle.Right), Category("Wen"), Description("关闭样式箭头方向")]
        public WenButtonTriangleStyle CloseStyle { get => closeStyle; set => closeStyle = value; }
        [DefaultValue(WenButtonTriangleStyle.Bottom), Category("Wen"), Description("打开样式箭头方向")]
        public WenButtonTriangleStyle OpenStyle { get => openStyle; set => openStyle = value; }

        [DefaultValue(false), Category("Wen"), Description("开关状态")]
        public bool State { get => state; set { state = value; this.Invalidate(); } }

        [Category("Wen")]
        [Description("按钮样式颜色")]
        [RefreshProperties(RefreshProperties.Repaint)]
        [DefaultValue(typeof(Color), "199,199,199")]
        public Color ButtonColor { get; set; } = Color.FromArgb(199, 199, 199);

        #endregion

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            this.Height = this.Width;
        }
        protected override void OnClick(EventArgs e)
        {
            if (State)
                State = false;
            else
                State = true;
            base.OnClick(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.BackColor = Color.Transparent;
        }
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.BackColor = Color.FromArgb(63, 63, 65);
        }
        protected override void WenOnPaint(Graphics g, Rectangle rec, PaintEventArgs e)
        {
            base.WenOnPaint(g, rec, e);

            if (State)
            {
                FillTriangle(OpenStyle, g, rec);
            }
            else
            {
                FillTriangle(CloseStyle, g, rec);
            }
        }

        private void FillTriangle(WenButtonTriangleStyle style, Graphics g, Rectangle rec)
        {
            rec = new Rectangle(rec.X, rec.Y, rec.Width - 1, rec.Height - 1);
            if (style == WenButtonTriangleStyle.Left)
                FillTriangleLeft(g, rec);
            if (style == WenButtonTriangleStyle.Right)
                FillTriangleRitht(g, rec);
            if (style == WenButtonTriangleStyle.Top)
                FillTriangTop(g, rec);
            if (style == WenButtonTriangleStyle.Bottom)
                FillTriangBottom(g, rec);
        }


        private void FillTriangleRitht(Graphics g, Rectangle rec)
        {
            Point point1 = new Point(rec.X, rec.Y);
            Point point2 = new Point(rec.X + rec.Width, rec.Y + rec.Height / 2);
            Point point3 = new Point(rec.X, rec.Y + rec.Height);
            GdiTriangLine(g, point1, point2, point3);
        }
        private void FillTriangleLeft(Graphics g, Rectangle rec)
        {
            Point point1 = new Point(rec.X + rec.Width, rec.Y);
            Point point2 = new Point(rec.X, rec.Y + rec.Height / 2);
            Point point3 = new Point(rec.X + rec.Width, rec.Y + rec.Height);
            GdiTriangLine(g, point1, point2, point3);
        }
        private void FillTriangBottom(Graphics g, Rectangle rec)
        {
            Point point1 = new Point(rec.X, rec.Y);
            Point point2 = new Point(rec.X + rec.Width / 2, rec.Y + rec.Y + rec.Height);
            Point point3 = new Point(rec.X + rec.Width, rec.Y);
            GdiTriangLine(g, point1, point2, point3);
        }
        private void FillTriangTop(Graphics g, Rectangle rec)
        {
            Point point1 = new Point(rec.X, rec.Y + rec.Height);
            Point point2 = new Point(rec.X + rec.Width / 2, rec.Y);
            Point point3 = new Point(rec.X + rec.Width, rec.Y + rec.Height);
            //Point[] pntArr = { point1, point2, point3 };
            //g.FillPolygon(GetBrush(ButtonColor), pntArr);
            GdiTriangLine(g, point1, point2, point3);
        }

        //三点画线条
        private void GdiTriangLine(Graphics g, Point point1, Point point2, Point point3)
        {
            using Pen pen = new Pen(new SolidBrush(ButtonColor), 1.6f);
            g.DrawLine(pen, point1, point2);
            g.DrawLine(pen, point2, point3);
        }
    }

    //三角形按钮方向
    public enum WenButtonTriangleStyle
    {
        Left = 1,
        Right = 2,
        Top = 3,
        Bottom = 4,
    }
}
